package com.thxr.sso.server.controller;

import com.thxr.sso.client.rpc.Result;
import com.thxr.sso.server.pojo.User;
import com.thxr.sso.server.service.UserService;
import com.thxr.sso.server.util.HMACSHA1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.DatatypeConverter;

/**
 * @Author: silence.Z
 * @CreateTime: 2020/11/9 11:54
 * @Description:
 */
@Controller
@RequestMapping("zimbra")
public class ZimbraController {
    //zimbraPreAuthKey
    private static final String ENCRYPTKEY = "5fa036fdb65d3828d8b772bc4c88db44a945a413f016c0ce51d8fe6ed1e4713d";

    /*@Resource(name = "skipVerifyRestTemplate")
    private RestTemplate skipVerifyRestTemplate;*/
    @Autowired
    private UserService userService;

    /*@GetMapping("login")
    @ResponseBody
    public String login(String userName) {

        String timestamp = String.valueOf(System.currentTimeMillis());
        System.out.println(timestamp);
        byte[] bytes = new byte[0];
        String preauth = null;
        ResponseEntity<String> forEntity = null;
        try {
            Result result = userService.findByAccount(userName);
            User user = (User) result.getData();
            String email = user.getEmail();
            System.out.println(email);

            bytes = HMACSHA1.HmacSHA1Encrypt(email+"|name|0|"+timestamp, ENCRYPTKEY);
            preauth = DatatypeConverter.printHexBinary(bytes);
            System.out.println(preauth);

            forEntity = skipVerifyRestTemplate.getForEntity("https://mail.jxthxr.com/service/preauth?account="+email+
                            "&by=name&expires=0&timestamp="+timestamp+"&preauth="+preauth,
                    String.class, new Object[] {});
        } catch (Exception e) {
            e.printStackTrace();
        }
        return forEntity.getBody();
    }*/

    @GetMapping("login")
    public String login(@RequestParam("userName")String userName, HttpServletResponse response) {

        String timestamp = String.valueOf(System.currentTimeMillis());
        System.out.println(timestamp);
        byte[] bytes = new byte[0];
        String preauth = null;
        String email = null;
        try {
            Result result = userService.findByAccount(userName);
            if (result.getCode() != 1){
                response.setContentType("text/javascript;charset=utf-8");
                response.getWriter().write(result.getMessage());
                return null;
            }
            User user = (User) result.getData();
            email = user.getEmail();
            System.out.println(email);

            bytes = HMACSHA1.HmacSHA1Encrypt(email+"|name|0|"+timestamp, ENCRYPTKEY);
            preauth = DatatypeConverter.printHexBinary(bytes);
            System.out.println(preauth);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:https://mail.jxthxr.com/service/preauth?account="+email+"&by=name&expires=0&timestamp="+timestamp+"&preauth="+preauth;
    }
}
